package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.OldInConstant;
import com.csun.cmny.provider.model.domain.OmcFollowUp;
import com.csun.cmny.provider.model.domain.OmcFollowUpLog;
import com.csun.cmny.provider.model.domain.OmcVisitor;
import com.csun.cmny.provider.model.dto.VisitorQueryDto;
import com.csun.cmny.provider.model.dto.visitor.visitorSaveDto;
import com.csun.cmny.provider.model.dto.visitor.visitorUpdateDto;
import com.csun.cmny.provider.model.vo.RpcRoleVo;
import com.csun.cmny.provider.model.vo.visitorVo.visitorVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcFollowUpLogService;
import com.csun.cmny.provider.service.OmcFollowUpService;
import com.csun.cmny.provider.service.OmcVisitorService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.provider.utils.PhoneUtil;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping(value = "/visitor", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcVisitorController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcVisitorService omcVisitorService;
    @Resource
    private OmcFollowUpService omcFollowUpService;
    @Resource
    private OmcFollowUpLogService omcFollowUpLogService;
    @Resource
    private UacRpcService uacRpcService;





    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "来访登记")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addVisitor(@RequestBody @Valid visitorSaveDto visitorSaveDto, BindingResult br) {



        OmcVisitor omcVisitor1 = new OmcVisitor();
        omcVisitor1.setPhone(visitorSaveDto.getPhone());
        omcVisitor1.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcVisitor> select = omcVisitorService.select(omcVisitor1);
        if(PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("visitor.phone.repeat", null));
        }

        //插入跟进信息
        OmcFollowUp omcFollowUp = new OmcFollowUp();
        BeanUtils.copyProperties(visitorSaveDto,omcFollowUp);
        omcFollowUp.setId(null);
        omcFollowUp.setSource(1);
        omcFollowUp.setGroupId(getLoginAuthDto().getGroupId());
        omcFollowUp.setUserId(getLoginAuthDto().getUserId());
        int save = omcFollowUpService.save(omcFollowUp);
        if(save>0){
            OmcVisitor visitor = new OmcVisitor();
            BeanUtils.copyProperties(visitorSaveDto,visitor);
            visitor.setFollowUpId(omcFollowUp.getId());
            visitor.setGroupId(getLoginAuthDto().getGroupId());
            visitor.setReceptionist(getLoginAuthDto().getUserName());
            visitor.setUserId(getLoginAuthDto().getUserId());
            int result = omcVisitorService.save(visitor);

            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
        }


        return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("operation.failed", null));
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "来访分页查询")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper select(@RequestBody VisitorQueryDto visitorQueryDto,BindingResult br){

        // 获取当前用户角色
        List<RpcRoleVo> roleVoList = uacRpcService.queryRoleListByUserId(getLoginAuthDto().getUserId());

        //如果角色是销售员,那么只能看自己录入的信息
        for (RpcRoleVo rpcRoleVo : roleVoList) {
            String roleCode = rpcRoleVo.getRoleCode();
            if(roleCode.equals(OldInConstant.SALESMAN)){
                visitorQueryDto.setUserId(getLoginAuthDto().getUserId());
            }
        }

        visitorQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        List<visitorVo>list=omcVisitorService.selectVisitor(visitorQueryDto);


            for (visitorVo visitorVo : list) {
                if(!visitorVo.getUserId().equals(getLoginAuthDto().getUserId())){
                    visitorVo.setIsMy(1);
                }
            }


        PageInfo<visitorVo> pageInfo = new PageInfo<>(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改来访信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper update(@RequestBody @Valid visitorUpdateDto visitorUpdateDto, BindingResult br){



        //校验此手机号是否在别的渠道登记过
        if(PublicUtil.isNotEmpty(visitorUpdateDto.getPhone())){
            OmcVisitor omcVisitor1 = new OmcVisitor();
            omcVisitor1.setPhone(visitorUpdateDto.getPhone());
            omcVisitor1.setGroupId(getLoginAuthDto().getGroupId());
            List<OmcVisitor> select = omcVisitorService.select(omcVisitor1);

            if(PublicUtil.isNotEmpty(select)&&!select.get(0).getId().equals(visitorUpdateDto.getId())){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("visitor.phone.repeat", null));
            }

        }


        OmcVisitor omcVisitor = new OmcVisitor();
        BeanUtils.copyProperties(visitorUpdateDto,omcVisitor);
        int result = omcVisitorService.update(omcVisitor);

        //修改回访跟进信息
        if(result>0){

            OmcVisitor visitor = omcVisitorService.selectByKey(omcVisitor.getId());
            OmcFollowUp omcFollowUp = omcFollowUpService.selectByKey(visitor.getFollowUpId());
            OmcFollowUp followUp = new OmcFollowUp();
            BeanUtils.copyProperties(omcVisitor,followUp);
            followUp.setId(omcFollowUp.getId());
            omcFollowUpService.update(followUp);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除来访信息")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper delete(Long id){


        OmcVisitor visitor = omcVisitorService.selectByKey(id);

        int result = omcVisitorService.deleteByKey(id);
        if(result>0&&PublicUtil.isNotEmpty(visitor)){

        //删除客户跟进信息
        omcFollowUpService.deleteByKey(visitor.getFollowUpId());

        //删除跟进日志信息
            OmcFollowUpLog omcFollowUpLog = new OmcFollowUpLog();
            omcFollowUpLog.setFollowUpId(visitor.getFollowUpId());
            omcFollowUpLogService.delete(omcFollowUpLog);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
}
